package com.aidex.biz.controller;

import com.aidex.biz.domain.ToHome;
import com.aidex.biz.service.*;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 预约上门Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2023-11-23
 */
@RestController
@RequestMapping("/biz/bizToHome")
public class ToHomeController extends BaseController {
    @Autowired
    private ToHomeService toHomeService;
    @Autowired
    private CustomerDongtaiService customerDongtaiService;
    @Autowired
    private CustomerShadowInfoService customerShadowInfoService;
    @Autowired
    private ToHomeUserService toHomeUserService;
    @Autowired
    private CustomerService customerService;

    /**
     * 查询我的预约上门列表 即上门门店客服看
     */
    @GetMapping("/list")
    public R<PageInfo> list(ToHome toHome, HttpServletRequest request, HttpServletResponse response) {
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            toHome.setToHomeCompanyId(null);
        } else {
            toHome.setToHomeCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        toHome.setPage(new PageDomain(request, response));
        return R.data(toHomeService.findPage(toHome));
    }


    /**
     * 查询预约上门列表
     */
    @GetMapping("/kefuList")
    public R<PageInfo> kefuList(ToHome toHome, HttpServletRequest request, HttpServletResponse response) {
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            toHome.setToHomeCompanyId(null);
        } else {
            toHome.setToHomeCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        toHome.setPage(new PageDomain(request, response));
        return R.data(toHomeService.findPage(toHome));
    }


    /**
     * 获取预约上门详细信息
     */
    @GetMapping(value = "/{id}")
    public R<ToHome> detail(@PathVariable("id") String id) {
        return R.data(toHomeService.get(id));
    }

    /**
     * 新增预约上门
     */
    @Log(title = "预约上门", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated ToHome toHome) {
        return R.status(toHomeService.save(toHome));
    }

    /**
     * 修改预约上门
     */
    @Log(title = "预约上门", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated ToHome toHome) {
        return R.status(toHomeService.save(toHome));
    }


    /**
     * 上门签到
     */
    @Log(title = "上门签到", businessType = BusinessType.UPDATE)
    @PostMapping("/qiandao")
    public R qiandao(@RequestBody @Validated ToHome toHome) {
        return R.status(toHomeService.Qiandao(toHome));
    }

    /**
     * 上门反馈
     */
    @Log(title = "上门反馈", businessType = BusinessType.UPDATE)
    @PostMapping("/fankui")
    public R fankui(@RequestBody @Validated ToHome toHome) {
        return R.status(toHomeService.Fankui(toHome));
    }


    /**
     * 删除预约上门
     */
    @Log(title = "预约上门", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(toHomeService.deleteToHomeByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(toHomeService.findMaxSort(new ToHome()));
    }

    /**
     * 导出预约上门列表
     */
    @Log(title = "预约上门", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(ToHome toHome) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            toHome.setCompanyId(null);
        } else {
            toHome.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<ToHome> list = toHomeService.findList(toHome);
        ExcelUtil<ToHome> util = new ExcelUtil<ToHome>(ToHome.class);
        return util.exportExcel(list, "预约上门数据");
    }

}
